<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="部门名称" prop="departmentNo">
                <el-select v-model="ruleForm.departmentNo" placeholder="请选择" class="w100p" clearable>
                    <el-option v-for="item, index in departmentList" :key="index" :label="item.departmentName"
                        :value="item.departmentNo">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="岗位名称" prop="postName">
                <el-input v-model="ruleForm.postName" class="w100p" clearable placeholder="请输入岗位名称"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="postDescription">
                <el-input type="textarea" v-model="ruleForm.postDescription" class="w100p" clearable
                    placeholder="请输入描述"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import departmentManage from '@/api/basicInformationOperate/departmentManage';
import stationManage from '@/api/basicInformationOperate/stationManage';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: stationManage.add,
            ruleForm: {},
            departmentList: [],
            rules: {
                departmentNo: [
                    { required: true, message: '请输入部门名称', trigger: 'change' }
                ],
                postName: [
                    { required: true, message: '请输入岗位名称', trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = stationManage.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = stationManage.edit
                this.addEditName = '编辑'
            };
            this.initOptionsData();
        },
        async initOptionsData() {
            let { data } = await departmentManage.queryByCondition({})
            this.departmentList = data || []
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        if (res.code == 1) {
                            this.cancel();
                            this.$message.success(res.msg || '操作成功')
                        } else {
                            this.$message.warning(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  